package com.example.chenchao.douyyin;

import android.app.Instrumentation;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Environment;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Configurator;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.util.Log;

import com.example.chenchao.douyyin.utils.BitmapUtils;
import com.example.chenchao.douyyin.utils.MathUtil;
import com.example.chenchao.douyyin.utils.NetWorkUtil;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.Response;

@RunWith(AndroidJUnit4.class)
public class Douyin2 {
    private Instrumentation mInstrumentation;
    private UiDevice mDevice;
//    private String token;

    @Before
    public void setUp(){
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mDevice = UiDevice.getInstance(mInstrumentation);
        Context context = InstrumentationRegistry.getContext();
        String pkName= context.getPackageName();
        Configurator configurator = Configurator.getInstance();
        long defalutWaitSelector = configurator.getWaitForSelectorTimeout();
        configurator.setWaitForSelectorTimeout(1000L);
        configurator.setActionAcknowledgmentTimeout(3000L);
        configurator.setScrollAcknowledgmentTimeout(3000L);
        configurator.setKeyInjectionDelay(100L);

    }



    @Test
    public void start()  {
//        token = OCRService.getAuth();
//        try {
//           String res = NetWorkUtil.escape("iVBORw0KGgoAAAANSUhEU g");
//           Log.e("res",res);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        File f = new File("/mnt/sdcard/DCIM/snapShot.jpg");
        mDevice.takeScreenshot(f);
        String absPath = f.getAbsolutePath();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Bitmap imageBitmap = BitmapFactory.decodeFile(absPath);
        Bitmap bitmap = Bitmap.createBitmap(600,400, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
       Paint mBitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                mBitPaint.setFilterBitmap(true);
         mBitPaint.setDither(true);
        canvas.drawBitmap(imageBitmap,new Rect(0,980,600,1160),new Rect(0,100,600,400),mBitPaint);
//        点赞
        canvas.drawBitmap(imageBitmap,new Rect(630,800,720,840),new Rect(0,60,90,100),mBitPaint);
//        评论
        canvas.drawBitmap(imageBitmap,new Rect(630,910,720,950),new Rect(100,60,190,100),mBitPaint);
//        转发
        canvas.drawBitmap(imageBitmap,new Rect(630,1020,720,1060),new Rect(200,60,290,100),mBitPaint);

        BitmapUtils.saveBitmap(bitmap,"tmp.jpg");
         int len = bitmap.getByteCount();
        
//        String b64Str = BitmapUtils.bitmapToBase64(bitmap);
           try {
//           String imgStr = b64Str;
               File file = new File("/mnt/sdcard/DCIM/", "tmp.jpg");
             String res =  OCRService.convertImg2String(file);
             Log.e("res",res);

        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("abc",mDevice.getDisplayWidth()+"|"+mDevice.getDisplayHeight()+"|"+imageBitmap.getWidth()+"|"+imageBitmap.getHeight());
    }

    private void page1() throws InterruptedException {
        UiObject2 object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/alk")),2000);
        if(object!=null){
            object.click();
            page2();
        }
    }
    //个人页面
    private void page2() throws InterruptedException {
        UiObject2 object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/ae6")),2000);
        if(object!=null){
            object.click();
            page3();
        }
    }

    private void page3() throws InterruptedException {
        UiObject2 object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/ag6")),2000);
        if(object!=null){
          List<UiObject2> children = object.getChildren();
          for(UiObject2 obj:children){
              obj.click();
              page4();
          }
        }
    }
    //我关注的个人主页
    private void page4() throws InterruptedException {
        UiObject2 object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/adj")),2000);
        if(object!=null){
            String authorName = object.getText();
        }
        object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/adl")),2000);
        if(object!=null){
            String authorId = object.getText();
        }
        object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/adv")),2000);
        if(object!=null){
            String personalSign = object.getText();
        }
        object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/adv")),2000);
        if(object!=null){
            String personalSign = object.getText();
        }
        object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/ae5")),2000);
        if(object!=null){
            String likeNum = object.getText();
            likeNum = MathUtil.converWString(likeNum);
        }
        object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/ae7")),2000);
        if(object!=null){
            String focusNum = object.getText();
            focusNum = MathUtil.converWString(focusNum);
        }

        object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/ae9")),2000);
        if(object!=null){
            String fansNum = object.getText();
            fansNum = MathUtil.converWString(fansNum);
        }
        object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/title").textStartsWith("作品")),2000);
        if(object!=null){
            String worksNum = object.getText().replace("作品 ","");
            worksNum = MathUtil.converWString(worksNum);
        }
        object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/adv").textStartsWith("动态")),2000);
        if(object!=null){
            String statusNum = object.getText().replace("动态 ","");
            statusNum = MathUtil.converWString(statusNum);
        }
        object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/adv").textStartsWith("喜欢")),2000);
        if(object!=null){
            String collectionNum = object.getText().replace("喜欢 ","");
            collectionNum = MathUtil.converWString(collectionNum);
        }
        getVideosInfo();
    }

    private void getVideosInfo() throws InterruptedException {
        UiObject2 object = mDevice.wait(Until.findObject(By.res("com.ss.android.ugc.aweme:id/te")),2000);
        if(object!=null){
            return;
        }

        List<UiObject2> children = object.getChildren();
        UiObject2 obj = children.get(0);
        obj.click();
        mDevice.wait(2000);
        mDevice.click(mDevice.getDisplayWidth()/2,mDevice.getDisplayHeight()/2);
        for(int i=0;i<children.size();i++){
            getVideoInfo();
            slideToNextVideo();
        }
    }

    private void getVideoInfo() {
        File f = new File(Environment.DIRECTORY_PICTURES+File.separator+"screenShot.png");
        mDevice.takeScreenshot(f);
        Bitmap imageBitmap = BitmapFactory.decodeFile(f.getPath());
        System.out.println(imageBitmap.getWidth()+"|"+imageBitmap.getHeight());
    }

    private void slideToNextVideo() {
        //滑到下一个视频
        try {
            int t = 2000;
            Thread.sleep(t);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int startY = mDevice.getDisplayHeight()*3/4;
        int endY = mDevice.getDisplayHeight()/4;
        mDevice.swipe(mDevice.getDisplayWidth()/2,startY,mDevice.getDisplayWidth()/2,endY,10);
        try {
            int t = 2000+(int)(Math.random()*5000);
            Thread.sleep(t);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
